<%--
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
--%>
<%@ include file="/taglibs.jsp" %>
<%@ tag import="fitnesse.wiki.WikiPage" %>
<%@ tag import="fitnesse.wikitext.WidgetBuilder" %>
<%@ tag import="fitnesse.wikitext.widgets.WidgetRoot" %>
<%@ tag import="org.xptracker.domain.WikiRenderer" %>
<%@ tag import="org.xptracker.domain.WikiContent" %>
<%@ tag import="org.xptracker.domain.WikiStyle" %>
<%@ attribute name="content" type="org.xptracker.domain.WikiContent" %>
<%@ attribute name="text" type="java.lang.String" %>
<%@ attribute name="maxLines" type="java.lang.Integer" %>

<c:if test="${!empty text}">
  <% jspContext.setAttribute("content", new WikiContent(text, WikiStyle.bare));%>
</c:if>

<c:choose>
<c:when test="${empty content}">
  <strong>{{content object is null}}</strong>
</c:when>
<c:otherwise>
<%
  WikiContent tagContent = (WikiContent) jspContext.getAttribute("content");
  boolean doExpansion = false;
  StringBuffer localContent = new StringBuffer();
  if (maxLines != null) {
    String[] lines = tagContent.getContent().split("\n");
    int lineLength = lines.length;
    for (int i = 0; i < Math.min(lineLength, maxLines); i++) {
      localContent.append(lines[i]).append("\n");
    }

    if (lineLength > maxLines) {
      doExpansion = true;
    }
  } else {
    localContent.append(tagContent.getContent());
  }

  WikiRenderer renderer = tagContent.getWikiStyle().getRenderer();
  jspContext.setAttribute("localContent", renderer.render(localContent.toString()));
  if (doExpansion) {
    jspContext.setAttribute("fullContent", renderer.render(tagContent.getContent()));
    jspContext.setAttribute("doExpansion", doExpansion);
  }
%>

<c:if test="${empty blogDivId}">
  <c:set var="blogDivId" value="0" scope="session"/>
</c:if>

<c:if test="${doExpansion}">
  <span style="display:none" id="full_blog_${blogDivId}">
    ${fullContent}
    <p style="margin-top:5px">[<a href="javascript:tweakBlog(${blogDivId})">collapse</a>]</p>
  </span>
  <span id="collapsed_blog_${blogDivId}">
</c:if>
${localContent}
<c:if test="${doExpansion}">
  <p>
  [<a href="javascript:tweakBlog(${blogDivId})">...</a>]
  </p>
  </span>
</c:if>
<c:set var="blogDivId" value="${blogDivId + 1}" scope="session"/>
</c:otherwise>
</c:choose>